import entity.User;
import service.UserService;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        UserService userService = new UserService();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("\n请选择操作：");
            System.out.println("1. 注册");
            System.out.println("2. 登录");
            System.out.println("3. 退出");
            System.out.print("请输入选项 (1/2/3): ");

            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符

            switch (choice) {
                case 1:
                    registerUser(userService, scanner);
                    break;
                case 2:
                    loginUser(userService, scanner);
                    break;
                case 3:
                    System.out.println("退出系统。");
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private static void registerUser(UserService userService, Scanner scanner) {
        User newUser = new User();

        System.out.print("请输入账号: ");
        newUser.setAccount(scanner.nextLine());

        System.out.print("请输入密码: ");
        newUser.setPassword(scanner.nextLine());

        System.out.print("请再次输入密码: ");
        String confirmPassword = scanner.nextLine();
        if (!confirmPassword.equals(newUser.getPassword())) {
            System.out.println("两次输入的密码不一致，请重新注册。");
            return;
        }

        System.out.print("请输入昵称: ");
        newUser.setNickname(scanner.nextLine());

        System.out.print("请输入性别 (0: 女性, 1: 男性): ");
        byte gender = Byte.parseByte(scanner.nextLine());
        newUser.setGender(gender);

        System.out.print("请输入身份证号: ");
        newUser.setIdCard(scanner.nextLine());

        System.out.print("请输入邮箱: ");
        newUser.setEmail(scanner.nextLine());

        System.out.print("请输入手机号: ");
        newUser.setPhone(scanner.nextLine());

        System.out.print("请输入用户类型 (0: 普通用户, 1: 管理员): ");
        byte userType = Byte.parseByte(scanner.nextLine());
        newUser.setType(userType);

        System.out.print("请输入 OpenID (可选，直接回车跳过): ");
        String openId = scanner.nextLine();
        newUser.setOpenId(openId.isEmpty() ? null : openId);

        boolean registered = userService.register(newUser);

        if (registered) {
            System.out.println("注册成功！");
        } else {
            System.out.println("注册失败！");
        }
    }

    private static void loginUser(UserService userService, Scanner scanner) {
        System.out.print("请输入账号: ");
        String account = scanner.nextLine();

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        User user = userService.login(account, password);

        if (user != null) {
            System.out.println("登录成功！");
            System.out.println(user);
        } else {
            System.out.println("登录失败！");
        }
    }
}
